#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QProcess>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    my_tcp->aes_tcp_init();
    connect(my_tcp,SIGNAL(read_msg(QByteArray)),this,SLOT(read_tcp_msg(QByteArray)));

    //init remote update module
    my_lus->init_prase();
    connect(my_tcp, SIGNAL(recive_lus_pack(QString)), my_lus, SLOT(lus_exec_pack(QString)));

    QProcess *myprocess = new QProcess();
    myprocess->start("bme680");

    wireless_port = new QSerialPort();
    gps_port = new QSerialPort();

    serial_init(gps_port, "/dev/ttyS4");
    serial_init(wireless_port, "/dev/ttyS3");
    startTimer(2000);
    timer = new QTimer();
    connect(timer,SIGNAL(timeout()),this,SLOT(send_urt_msg()));
    timer->start(2000);

}


void MainWindow::serial_init(QSerialPort * port, QString name)
{


    port->setPortName(name);

    if(port->open(QIODevice::ReadWrite))
    {
        port->setBaudRate(QSerialPort::Baud9600);
        port->setDataBits(QSerialPort::Data8);
        port->setParity(QSerialPort::NoParity);
        port->setStopBits(QSerialPort::OneStop);
        port->setFlowControl(QSerialPort::NoFlowControl);
    }
    else {
        qDebug()<<"open uart fail:" <<name;
        return;
    }

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::timerEvent(QTimerEvent *event)
{
    QFile file("bme680.dat");

    //file->setFileName("bme680.dat");
    file.open(QIODevice::ReadOnly|QIODevice::Text);
    if(file.isOpen())
    {
        QString bme_680 = file.readAll();
        //qDebug()<<bme_680;

        my_tcp->lus_send_user_msg("bme680_data","",data);
        bme_680 = bme_680.simplified();
        QStringList bme_list =  bme_680.split('#');

        for(int i=0; i<bme_list.size(); i++){
            qDebug()<<bme_list.at(i);
        }
        data = bme_680.toUtf8();
        data = data.insert(0, '[');
        data.append(']');
        wireless_port->write(data);
    }
    else {
        qDebug()<<"Can't open the file!!";
    }
}


void MainWindow::send_lus_cmd(QByteArray cmd)
{
  //send bord info to server
  my_tcp->sendMessage(cmd, ENCRYPT_YES);

}

void MainWindow::read_tcp_msg(QByteArray data1)
{
    my_tcp->debug_switch = true;
    qDebug()<<"haha" <<data1;

    QDomDocument xml;
    xml.setContent(data1);

    QDomElement rootnode = xml.documentElement();

     if(rootnode.tagName() == "init_msg"){

       qDebug() << "msg: connect lus server success!"<<my_tcp->fix_id;

       // enable send bord info
       connect(my_lus, SIGNAL(lus_requst_send(QByteArray)), this , SLOT(send_lus_cmd(QByteArray)));


       return;
   }

    if(rootnode.tagName() == "lus_user_msg"){
        if(rootnode.attributeNode("type").value() == "user_data"){
            QString bme_data = rootnode.attributeNode("body").value();
            qDebug()<<bme_data;
            QString answer_data = data;
            my_tcp->lus_send_user_msg("answer_data","",answer_data);
        }
        //QString answer_data = "haha";
        //my_tcp->lus_send_user_msg("answer_data","",answer_data);
    }
}

void  MainWindow::send_urt_msg()
{
    data2 = gps_port->readAll();
    //qDebug()<<data2<<"END FLAG#";

    if(data2.contains("$GPRMC",Qt::CaseSensitive))
    {
        QString x = "$GPRMC";
        QString y = "$GPVTG";
        int index_point_x = 0;
        int index_point_y = 0;
        index_point_x = data2.indexOf(x);
        index_point_y = data2.indexOf(y);
        qDebug()<<index_point_x<<index_point_y;
        if(index_point_x != -1 && index_point_y != -1)
        {
            double be_time;
            QString world_time;
            QString time;
            QString lat;
            QString lon;
            QString lat_location;
            QString lon_location;
            QString gprmc_data1;
            QString gprmc_data2;
            gps_data = data2.mid(index_point_x,index_point_y-index_point_x);
            qDebug()<<gps_data;

            //QStringList gps_list = gps_data.split(',');

            be_time = (gps_data.section(',',1,1)).toDouble();
            world_time = QString::number(be_time);
            //qDebug()<<be_time<<world_time;
            lat = gps_data.section(',',3,3);
            lat_location = gps_data.section(',',4,4);
            lon = gps_data.section(',',5,5);
            lon_location = gps_data.section(',',6,6);
            time = gps_data.section(',',9,9);
            gprmc_data1 = QString("%1,%2,%3,%4%5").arg(world_time).arg(lat).arg(lon).arg(time).arg("$");
            gprmc_data2 = QString("%1%2,%3%4").arg(lat_location).arg(lat).arg(lon_location).arg(lon);
            my_tcp->lus_send_user_msg("gps_data","",data4);
            qDebug()<<gprmc_data1;

            data3 = gprmc_data1.toUtf8();
            data4 = gprmc_data2.toUtf8();
            wireless_port->write(data3);
            gps_data.clear();
            data2.clear();
        }
        else {
            return;
        }
    }
    else {
        return;
    }

}
